
using System;
using System.Collections.Concurrent;
using System.IO;
using UMC.Data;
using UMC.ITME;
using UMC.ITME.Entities;
using UMC.Net;
namespace UMC.ITME
{
    class HttpCheckDoWorker : UMC.Host.IDoWorker
    {
        ConcurrentQueue<Tuple<Cookie, SiteConfig>> _cookeis = new ConcurrentQueue<Tuple<Cookie, SiteConfig>>();

        void Do()
        {
            while (_cookeis.TryDequeue(out var v))
            {
                var url = new Uri(new Uri(v.Item2.Domains[0]), v.Item2.GetHome());
                var req = url.WebRequest();
                if (v.Item1.CookieValue?.Length > 0)
                {
                    var cookieValues = new CookieSet(v.Item1.CookieValue);
                    var ms = new HeadValues();
                    cookieValues.WirteValue(ms, new StringValue(url.Host), new StringValue(url.LocalPath));
                    int len = ms.BytesSize + ms.Span.Length * 3;
                    var cacheValu = System.Buffers.ArrayPool<byte>.Shared.Rent(len);// byte[len];//ms.BytesSize + ms.Span.Length * 3];
                    var cacheValue = cacheValu.AsSpan();
                    var size = 0;
                    foreach (var ck in ms.Span)
                    {
                        ck.Name.Span.CopyTo(cacheValue.Slice(size));
                        size += ck.Name.Length;
                        cacheValue[size] = 61;
                        size++;
                        ck.Value.Span.CopyTo(cacheValue.Slice(size));
                        size += ck.Value.Length;
                        cacheValue[size] = 59;
                        size++;
                        cacheValue[size] = 32;
                        size++;
                    }
                    req.Headers[StringValue.Cookie] = new StringValue(cacheValue.Slice(0, size - 2).ToArray());
                    System.Buffers.ArrayPool<byte>.Shared.Return(cacheValu);
                    req.Get(r =>
                    {
                        DataFactory.Instance().Put(new Cookie { Domain = v.Item1.Domain, user_id = v.Item1.user_id, IndexValue = v.Item1.IndexValue, Time = DateTime.Now });
                        r.ReadAsData((b, c, l) =>
                        {
                            if (c == 0 && b.Length == 0)
                            {
                                Do();
                            }
                        });
                    });
                }
            }
        }
        public void DoWork()
        {
            var sites = DataFactory.Instance().SiteConfig();
            foreach (var s in sites)
            {
                if (s.CheckTime > 0 && s.Domains[0].Contains("*") == false)
                {
                    int index = 0;
                    while (index > -1)
                    {
                        var cookies = HotCache.Cache<Cookie>().Find(new Entities.Cookie { Domain = s.Root }, index, 3000, out index);
                        foreach (var c in cookies)
                        {
                            if (c.CookieValue?.Length > 0 && c.Time.Value.AddMinutes(s.CheckTime) < DateTime.Now)
                            {
                                _cookeis.Enqueue(Tuple.Create(c, s));
                            }
                        }

                    }
                    Do();
                }
            }

            HttpMimeServier.Register(300, this);
        }
    }
}
